package com.bz.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bz.content.service.ContentService;
import com.bz.mapper.TbContentMapper;
import com.bz.pojo.TbContent;
import com.bz.utils.Builder;
import com.bz.utils.PageResult;
import com.bz.vo.ContentVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ContentServiceImpl extends ServiceImpl<TbContentMapper, TbContent> implements ContentService {

    //@Value("${frontend.AD}")
    private Long AD = 89L;

    @Override
    public PageResult selectTbContentAllByCategoryId(Integer page, Integer size,Long categoryId) {
        QueryWrapper<TbContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(categoryId != null,TbContent::getCategoryId,categoryId);
        Page<TbContent> pageInfo = PageHelper.startPage(page, size).doSelectPage(() -> this.baseMapper.selectList(queryWrapper));
        return new PageResult(pageInfo.getPageNum(),pageInfo.getPages(),pageInfo.getResult());
    }

    @Override
    public Integer insertTbContent(TbContent tbContent) {
        tbContent.setCreated(new Date());
        tbContent.setUpdated(new Date());
        return this.baseMapper.insert(tbContent);
    }

    @Override
    public Integer deleteContentByIds(Long id) {
        return this.baseMapper.deleteById(id);
    }

    @Override
    public List<ContentVo> selectFrontendContentByAD() {
        QueryWrapper<TbContent> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbContent::getCategoryId,this.AD);
        List<TbContent> tbContentList = this.baseMapper.selectList(wrapper);
        return tbContentList.stream().map(x ->
            Builder.of(ContentVo::new)
                   .with(ContentVo::setSrc, x.getPic())
                   .with(ContentVo::setSrcB, null)
                   .with(ContentVo::setAlt, x.getSubTitle())
                   .with(ContentVo::setHref, x.getUrl())
                   .with(ContentVo::setWidth, 670)
                   .with(ContentVo::setWidthB, 550)
                   .with(ContentVo::setHeight, 240)
                   .with(ContentVo::setHeightB, 240)
                   .build()
        ).collect(Collectors.toList());
    }
}
